package com.ruoyi.finance.collection.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.finance.collection.domain.BillCollection;
import com.ruoyi.finance.collection.domain.VBillCollection;
import com.ruoyi.finance.collection.service.BillCollectionService;
import com.ruoyi.statement.domain.BillStatement;
import com.ruoyi.statement.service.IBillStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 针对表【BILL_COLLECTION(收款费项表)】的数据库操作Controller
 * @author 毕京瑞
 * @createTime 2023-07-04 15:03
 */
@RestController
@RequestMapping("/finance/billCollection")
public class BillCollectionController extends BaseController {
	@Autowired
	private BillCollectionService billCollectionService;

	@Autowired
	private IBillStatementService billStatementService;
	
	
	/**
	 * 根据参数获取【BILL_COLLECTION】列表
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/list")
	public TableDataInfo list(BillCollection billCollection) {
		startPage();
		List<BillCollection> list = billCollectionService.listByParams(billCollection);
		return getDataTable(list);
	}
	
	
	/**
	 * 根据id获取【BILL_COLLECTION】详细信息
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable Long id) {
		return AjaxResult.success(billCollectionService.getById(id));
	}
	
	
	/**
	 * 新增【BILL_COLLECTION】
	 */
	@PostMapping
	@Log(title = "BILL_COLLECTION表新增费项", businessType = BusinessType.INSERT)
	public AjaxResult add(@RequestBody BillCollection billCollection) {
		billCollectionService.setBasicFeeCodeAndName(billCollection);
		return AjaxResult.success(billCollectionService.save(billCollection));
	}
	
	/**
	 * 修改【BILL_COLLECTION】
	 */
	@PutMapping
	@Log(title = "BILL_COLLECTION表修改费项", businessType = BusinessType.UPDATE)
	public AjaxResult edit(@Validated @RequestBody BillCollection billCollection) {
		Long billStatementId = billCollection.getStatementId();
		if (billStatementId!=null){
			String confirmStatus = "1";
			String collectStatus = "2";
			BillStatement billStatement = billStatementService.getById(billStatementId);
			BillCollection billCollection1 = billCollectionService.getById(billCollection.getBillCollectionId());
			if(confirmStatus.equals(billStatement.getStatementStatus())){
				return AjaxResult.error("该对账单已确认,无法进行修改");
			}
			if(collectStatus.equals(billStatement.getStatementStatus())){
				return AjaxResult.error("该对账单已收款，无法进行修改");
			}
			if (!billCollection1.getCustomerNam().equals(billCollection.getCustomerNam())){
				UpdateWrapper<BillCollection> wrapper=new UpdateWrapper<>();
				wrapper.eq("STATEMENT_ID",billStatementId);
				wrapper.set("CUSTOMER_NAM",billCollection.getCustomerNam());
				billCollectionService.updateById(billCollection);
				return AjaxResult.success(billCollectionService.update(null,wrapper));
			}else {
				return AjaxResult.success(billCollectionService.updateById(billCollection));
			}
		}
			return AjaxResult.success(billCollectionService.updateById(billCollection));
	}
	
	/**
	 * 删除【BILL_COLLECTION】
	 */
	@DeleteMapping("/{ids}")
	@Log(title = "BILL_COLLECTION表删除费项", businessType = BusinessType.DELETE)
	public AjaxResult remove(@PathVariable Long[] ids) {
		// 获得删除时间
		Date deleteDate = DateUtils.getNowDate();
		// 获得删除操作人
		String deleteName = SecurityUtils.getUsername();
		for (Long id : ids){
			BillCollection billCollection = billCollectionService.getById(id);
			if (billCollection.getCollectionStatus().equals("3")){
				return AjaxResult.error("该费项已确认收款，不允许删除！");
			} else if (billCollection.getCollectionStatus().equals("2")){
				return AjaxResult.error("该费项已确认对账，请先取消确认对账后再进行删除！");
			} else if (billCollection.getCollectionStatus().equals("1")){
				UpdateWrapper<BillStatement> billStatementUpdateWrapper = new UpdateWrapper<>();
				billStatementUpdateWrapper.eq("BILL_STATEMENT_ID",billCollection.getStatementId());
				billStatementUpdateWrapper.set("DEL_FLAG","2");
				billStatementUpdateWrapper.set("DEL_TIME",deleteDate);
				billStatementUpdateWrapper.set("DEL_NAME",deleteName);
				billStatementService.update(billStatementUpdateWrapper);
				billCollectionService.updateDelNameAndDelTime(billCollection.getBillCollectionId(),deleteName,deleteDate);
			} else {
				billCollectionService.updateDelNameAndDelTime(billCollection.getBillCollectionId(),deleteName,deleteDate);
			}
		}
		
		return AjaxResult.success(billCollectionService.removeByIds(Arrays.asList(ids)));
	}

	/**
	 * 自动计费
	 * @param projectId
	 * @return AjaxResult
	 */
	@PostMapping("/autoCollection")
	@Log(title = "单条数据自动计费", businessType = BusinessType.INSERT)
	public AjaxResult autoCollection(@RequestBody Long projectId) {
		return billCollectionService.autoCollection(projectId);
	}


	/**
	 * 自动计费
	 * @param selectedProjectIds
	 * @return AjaxResult
	 */
	@PostMapping("/autoCollectionall")
	@Log(title = "勾选数据自动计费", businessType = BusinessType.INSERT)
	public AjaxResult autoCollectionall(@RequestBody Long[] selectedProjectIds) {
		return billCollectionService.autoCollectionall(selectedProjectIds);
	}

	/**
	 * get 根据对账单statementId查询收款费项（通用接口），获取到List<BillCollectionVo>
	 * @param statementId
	 */
	@GetMapping("/listVBCollectionByStatementId/{statementId}")
	public AjaxResult listVBCollectionByStatementId(@PathVariable Long statementId){
		List<VBillCollection> res = billCollectionService.listVBCollectionByStatementId(statementId);
		return AjaxResult.success(res);
	}


}

